import { directionEnum } from './../enum/directionEnum'
import modelAbstract from './modelAbstract'
import { image } from '../service/image'
import bullet from '../canvas/bullet'
import config from '../config'
import utils from '../utils'
import wall from '../canvas/wall'
import steel from '../canvas/steel'
import boss from '../canvas/boss'
import tank from '../canvas/tank'
import play from '../canvas/play'

export default class extends modelAbstract implements IModel {
  canvas: ICanvas = bullet
  name: string = 'bullet'
  constructor(public tank: IModel) {
    super(tank.x + config.model.width / 2, tank.y + config.model.height / 2)
    this.direction = tank.direction as unknown as directionEnum
  }
  image(): HTMLImageElement {
    return image.get('bullet')!
  }
  render(): void {
    let y = this.y
    let x = this.x
    switch (this.direction) {
      case directionEnum.top:
        y -= 5
        break
      case directionEnum.right:
        x += 5
        break
      case directionEnum.bottom:
        y += 5
        break
      case directionEnum.left:
        x -= 5
        break
    }
    //碰撞检测
    const touchModel = utils.isModelTouch(x, y, 2, 2, [...wall.models, ...steel.models, ...boss.models, ...tank.models, ...play.models])
    if (utils.isCanvasTouch(x, y, 2, 2)) {
      this.destory()
    } else if (touchModel && touchModel.name !== this.tank.name) {
      this.destory()
      if (touchModel.name != 'steel') touchModel.destory()
      this.blast(touchModel)
    } else {
      this.y = y
      this.x = x

      this.draw()
    }
  }
  draw() {
    this.canvas.ctx.drawImage(this.image(), this.x, this.y, 2, 2)
  }
}
